//
//  SHSearch2ViewController.m
//  youjian03
//
//  Created by 常琼 on 14/10/20.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHSearch2ViewController.h"
#import "SHchangeLocationTableViewCell.h"//自定义cell-搜索cell



@interface SHSearch2ViewController ()<BMKPoiSearchDelegate>
@property(nonatomic,strong)BMKPoiSearch *searcher;



@end



@implementation SHSearch2ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //self.title = @"更改位置";
    
    mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    mySearchBar.delegate = self;
    [mySearchBar setPlaceholder:@"输入新的地址"];
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"changelocation"]];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"back" highImageName:@"back" target:self action:@selector(backoff)];
    
    
    searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:mySearchBar contentsController:self];
    searchDisplayController.active = NO;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
	self.tableView.tableHeaderView = mySearchBar;
    
    [MBProgressHUD showMessage:@"请稍后..."];
    
    
    //百度搜索
    [self searchAddressWithLocation];
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//;

    
    
    
    
}

-(void)backoff
{
    [self.navigationController popViewControllerAnimated:YES];

    
    
    
}
/**
 *  初始化查询
 */
-(void)searchAddressWithLocation
{
    //初始化检索对象
    self.searcher =[[BMKPoiSearch alloc]init];
    self.searcher.delegate = self;
    //发起检索
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = 1 ;
    option.pageCapacity = 10;
    
    option.location = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    HMLog(@"经纬度1:%f,%f",self.latitude,self.longitude);
    option.keyword = @"酒店";
    BOOL flag = [_searcher poiSearchNearBy:option];
    
    if(flag)
    {
        HMLog(@"周边检索发送成功");
    }
    else
    {
        HMLog(@"周边检索发送失败");
        [MBProgressHUD hideHUD];
    }
    
    
}
#pragma mark -BMKPoiSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        [MBProgressHUD hideHUD];
        HMLog(@"BMKPoiResult count is %d",poiResultList.poiInfoList.count);
        NSMutableArray *array = [NSMutableArray array];
        NSMutableArray *arraycity = [NSMutableArray array];
        for (int i = 0; i<poiResultList.poiInfoList.count; i++) {
//            HMLog(@"name is %@, address is %@,lat->%f,lont->%f",((BMKPoiInfo *)poiResultList.poiInfoList[i]).name,((BMKPoiInfo *)poiResultList.poiInfoList[i]).address, ((BMKPoiInfo *)poiResultList.poiInfoList[i]).pt.latitude,((BMKPoiInfo *)poiResultList.poiInfoList[i]).pt.longitude);
            [array addObject:((BMKPoiInfo *)poiResultList.poiInfoList[i]).address];
            [arraycity addObject:((BMKPoiInfo *)poiResultList.poiInfoList[i]).city];
        }
        
        dataArray  = [array mutableCopy];
        cityArray = [arraycity mutableCopy];
        
        [self.tableView reloadData];
        
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        
        HMLog(@"起始点有歧义");
        MBProgressHUD  *HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        HUD.labelText =@"未查询到结果";
        HUD.mode = MBProgressHUDModeText;
        [HUD showAnimated:YES whileExecutingBlock:^{
            sleep(1.5);
        } completionBlock:^{
            [HUD removeFromSuperview];
        }];
    } else {
        HMLog(@"抱歉，未找到结果");
        MBProgressHUD  *HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        HUD.labelText =@"未查询到结果";
        HUD.mode = MBProgressHUDModeText;
        [HUD showAnimated:YES whileExecutingBlock:^{
            sleep(1.5);
        } completionBlock:^{
            [HUD removeFromSuperview];
        }];
    }
}



#pragma UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return searchResults.count;
    }
    else {
        return dataArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    SHchangeLocationTableViewCell *cell = (SHchangeLocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[SHchangeLocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        cell.title.text = dataArray[indexPath.row];
        cell.subtitle.text = cityArray[indexPath.row];
    }
    
    
    
    
    return cell;
}

#pragma mark -TableviewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //点击cell
    
    [self.celltext setTitle:[NSString stringWithFormat:@"我在 %@",dataArray[indexPath.row]] forState:UIControlStateNormal];
    
    
    
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}

#pragma UISearchDisplayDelegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    
    
    
    HMLog(@"文字改变");
}

@end
